package com.penghk.earth.util;

import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.util.encoders.Hex;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MDUtils {

    public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        //方式一
        MessageDigest digest = MessageDigest.getInstance("MD5");
        String word = "123456";
        byte[] dig = digest.digest(word.getBytes("utf-8"));
        byte[] endig = Hex.encode(dig);
        String digStr = new String(endig);
        System.out.println(digStr);

        //方式二
        System.out.println(new String(Hex.encode(DigestUtils.md5(word))));

        //方式三
        System.out.println(DigestUtils.md5Hex(word));

        //-----------------------------------------------------------------

        //SHA与MD5算法的不通之处主要在于摘要的长度，sha : 160bit md : 128bit
        //sha-1 对最大长度264byte的信息进行摘要 生成160bit
        //sha-224 sha-256 sha-384 sha-512 以其摘要信息bit长度命名

        MessageDigest digest1 = MessageDigest.getInstance("SHA");//SHA-1
        byte[] dig1 = digest1.digest(word.getBytes());
        byte[] endig1 = Hex.encode(dig1);
        String digStr1 = new String(endig1);
        System.out.println(digStr1);

        System.out.println(DigestUtils.shaHex(word));

//        MessageDigest digest2 = MessageDigest.getInstance("SHA-256");//SHA-256
        MessageDigest digest2 = MessageDigest.getInstance("SHA-512");//SHA-256
        byte[] dig2 = digest2.digest(word.getBytes());
        byte[] endig2 = Hex.encode(dig2);
        String digStr2 = new String(endig2);
        System.out.println(digStr2);

    }
}
